<?php

//小站选项应用模型Video模块
class Video_model extends Model {

	function Video_model()
	{
		parent::Model();	
		$this->load->database();
		$this->load->model("plugins_model", "pm");
		$this->load->model("comment_model");
		$this->load->model("options_model");
	}
	
	//获取列表
	function listitem($offset = 0, $pagecount = 20, $order = "id", $desc = "desc")
	{
		$items = array();
		$query = $this->db->from("module_video")->limit($pagecount, $offset)->order_by($order, $desc)->get();
		if ($query->num_rows()>0) {
			$items = $query->result_array();
			foreach($items as $k => $list){
				$siteid = $this->pm->get_siteid($list["plid"]);
				$site = $this->sm->get($siteid);
				if($site) {
					$items[$k]["domain"] = $site["domain"];
					$items[$k]["sitename"] = $site["name"];
					$items[$k]["logo"] = $site["logo"];
				} else {
					$items[$k]["domain"] = $items[$k]["sitename"] = $items[$k]["logo"] = "";
				}
				
				//用户信息
				$userid = $list["userid"];
				$userinfo = $this->am->get($userid);
				$items[$k]["userinfo"] = $userinfo;
			}
		}
		return $items;
	}
	
	//计算总数
	function countitems()
	{
		return $this->db->count_all_results("module_video");
	}
	
	//添加
	function add($plid)
	{
		//获取视频地址+截图
		$return = $this->get_videoinfo(p("link"));
//		a($return);
		if(empty($return) || !in_array($return['host'], array('ku6','youku', 'tudou', '56'))) {
			show_failed("视频链接输入错误");
		}
		
		/*$link = $this->getlink(p("link"));
		if($link == "") {
			show_failed("视频链接输入错误");
		}*/
		$data = array(
			"userid" => $this->userid,
			"title" => p("title"),
			"link" => $return['link'],
			"pic" => $return['imageurl'],
			"plid" => $plid,
			"addtime" => time(),
			"host" => $return['host'],
			"url" => p("link"),
			"VideoIDS" => $return['flashvar'],
		);
		$result = $this->db->insert("module_video", $data);
		$insert_id = $this->db->insert_id();
		return $insert_id;
	}
	
	//编辑
	function edit($plid)
	{
		$return = $this->get_videoinfo(p("link"));
		if(empty($return) || !in_array($return['host'], array('ku6','youku', 'tudou', '56'))) {
			show_failed("视频链接输入错误");
		}
		$id = p("id");
		$data = array(
			"userid" => $this->userid,
			"title" => p("title"),
			"link" => $return['link'],
			"pic" => $return['imageurl'],
			"plid" => $plid,
			"addtime" => time(),
			"host" => $return['host'],
			"url" => p("link"),
			"VideoIDS" => $return['flashvar'],
		);
		$this->db->where("id", $id);
		$result = $this->db->update("module_video", $data);
		return $result;
	}
	
	//删除
	function delete($dataid)
	{
		$result = $this->db->delete("module_video", array("id"=>$dataid));
		return $result;
	}
	
	//删除选择
	function delselected($delstr)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		//删除评论
//		$this->comment_model->forum_delete($id);
		$this->db->where_in("dataid", $delid);
		$this->db->delete("comment");
		
		$this->db->where_in("id", $delid);
		$result = $this->db->delete("module_video");
		return $result;
	}
	
	//#获取列表
	function lists($plid, $limit = 3, $offset = 0)
	{
		$lists = array();
		$query = $this->db->from("module_video")->where(array("plid"=>$plid))->order_by("id","desc")->limit($limit, $offset)->get();
		if($query->num_rows()>0) {
			$lists = $query->result_array();
			foreach ($lists as $k => $row) {
				//模块ID
				$plid = $row["plid"];
				$r = $this->pm->get($plid);
				$lists[$k]["moduleid"] = $r["moduleid"];
				//评论总数
				$opid = $r["opid"];
				$r = $this->options_model->get($opid);
				$siteid = $r["siteid"];
				$commentcount = $this->comment_model->count_lists($siteid, $plid, $row["id"]);
				$lists[$k]["commentcount"] = $commentcount;
			}
		}
		return $lists;
	}
	
	//计算总数
	function count_lists($plid)
	{
		$query = $this->db->from("module_video")->where(array("plid"=>$plid))->get();
		return $query->num_rows();
	}
	
	//#查看内容
	function show($id)
	{
		$row = array();
		$query = $this->db->get_where("module_video", array("id"=>$id));
		if($query->num_rows()>0) {
			$row = $query->row_array();
			//小站信息
			$r = $this->pm->get($row["plid"]);
			$opid = $r["opid"];
			$r = $this->options_model->get($opid);
			if ($r) {
				$siteid = $r["siteid"];
				$row["siteid"] = $siteid;
				//小站信息
				$row["site"] = $this->sm->get_site($siteid);
			} else {
				$row["site"]["name"] = '';
			}
			//用户信息
			$userid = $row["userid"];
			$userinfo = $this->am->get($userid);
			$row["userinfo"] = $userinfo;
		}
		return $row;
	}
	
	//#查看内容
	function get_info($id)
	{
		$row = array();
		$query = $this->db->get_where("module_video", array("id"=>$id));
		if($query->num_rows()>0) {
			$row = $query->row_array();
		}
		return $row;
	}
	
	//获取播放地址
	function getlink($url)
	{
		//获取播放地址
		$link = "";
		//56
		preg_match("/http:\/\/www\.56\.com\/(.*)\/(.*)\.html/i", $url, $matches);
		if(isset($matches[2]) && $matches[2] != "") {
			$link = "http://player.56.com/".$matches[2].".swf";
		}
		//youku
		if($link == "") {
			$contents = file_get_contents($url);
			preg_match("/http:\/\/player\.youku\.com\/player\.php(.*)\.swf/i", $contents, $matches);
			if(isset($matches[0]) && $matches[0] != "") {
				$link = $matches[0];
			} 
		}
		//ku6
		if($link == "") {
			preg_match("/\"http:\/\/player\.ku6\.com\/refer\/(.*)\.swf\"/i", $contents, $matches);
			if(isset($matches[1]) && $matches[1] != "") {
				$link = "http://player.ku6.com/refer/".$matches[1].".swf";
			} 
		}
		//tudou
		if($link == "") {
			$lid_code = $defaultIid = "";
			preg_match("/_code = '(.*)'/i", $contents, $matches);
			if(isset($matches[1]) && $matches[1] != "") {
				$lid_code = $matches[1];
			} 
			preg_match("/defaultIid = (.*)/i", $contents, $matches);
			if(isset($matches[1]) && $matches[1] != "") {
				$defaultIid = $matches[1];
			} 
			if($lid_code != "" && $defaultIid != "") {//播放地址类型1
				$link = "http://www.tudou.com/l/".$lid_code."/&iid=".$defaultIid."/v.swf";
			} elseif($lid_code != "") {//播放地址类型2
				$link = "http://www.tudou.com/v/".$lid_code."/v.swf";
			}
		}
		return $link;
	}
	
	//获取视频地址+截图
	//http://blog.csdn.net/nodbrag/archive/2011/03/21/6264243.aspx
	function get_videoinfo($url)
	{
		$return = array();
		$return['imageurl'] = $return['link'] = $return['flashvar'] = "";
		preg_match("/\.(\w+)\.com/i", $url, $matches);
		if(!empty($matches[1])) { 
			$host = $matches[1];
			$return['host'] = $host;
		}
		switch ($host){
			case "ku6":
				$contents = file_get_contents($url);
				preg_match_all("/\/([\w\-]+)\.html/", $url, $matches);
				if(1 > preg_match("/\/index_([\w\-]+)\.html/", $url) && !empty($matches[1][0])) {
					$return['flashvar'] = $matches[1][0];
				}else{
					preg_match_all("/refer\/(.*)\/v.swf/",$contents, $matches);
					$return['flashvar'] = $matches[1][0];
				}
				if(!empty($return['flashvar'])) {
					$return['link'] = 'http://player.ku6.com/refer/'.$return['flashvar'].'/v.swf';
				}
				//经分析，酷六的视频截图地址在视频页面的<span class="s_pic“></span>标签之间
				preg_match_all("/<span class=\"s_pic\">(.*)<\/span>/",$contents,$imageurl);
				if (!empty($imageurl[1][0])) {
					$return['imageurl'] = $imageurl[1][0];
				}
			break;
			case "youku":
				$contents = file_get_contents($url);
				preg_match_all("/id\_(\w+)[\=|.html]/", $url, $matches);
				if(!empty($matches[1][0])) {
					$return['flashvar'] = $matches[1][0];
				}
				if(!empty($return['flashvar'])) {
					$return['link'] = 'http://player.youku.com/player.php/sid/'.$return['flashvar'].'/v.swf';
				}
				//经分析，视频的截图的图片地址在该视频页面html代码里以<li class="download"></li>标记里的最后一个http://vimg
				preg_match_all("/<li class=\"download\"(.*)<\/li>/",$contents,$match2);
				preg_match("/http:\/\/g(.*).ykimg.com(.*)\|\"\>/i",$match2[1][0 ],$imageurl);
				if (!empty($imageurl[1])) {
					$return['imageurl'] = 'http://g'.$imageurl[1].'.ykimg.com'.$imageurl[2];
				}
			break;
			case "tudou":
				$contents = file_get_contents($url);
				preg_match_all("/view\/([\w\-]+)\/?/", $url, $matches);
				if(!empty($matches[1][0])) {
					$return['flashvar'] = $matches[1][0];
				}
				if(!empty($return['flashvar'])) {
					$return['link'] = 'http://www.tudou.com/v/'.$return['flashvar'].'/v.swf';
				}
				preg_match("/thumbnail(.*)= '(.*)'/",$contents,$imageurl);
				if (!empty($imageurl[2])) {
					$return['imageurl'] = $imageurl[2];
				}
			break;
			case "56":
				$contents = file_get_contents($url);
				preg_match("/http:\/\/www\.56\.com\/(.*)\/(.*)\.html/i", $url, $matches);
				if(isset($matches[2]) && $matches[2] != "") {
					$return['flashvar'] = $matches[2];
					$return['link'] = "http://player.56.com/".$matches[2].".swf";
				}
				preg_match('/"img":"(.*)"/', $contents, $imageurl);
				if (!empty($imageurl[1])) {
					$return['imageurl'] = str_replace("\\","", $imageurl[1]);
				}
			break;
		}
//		a($return);
		return $return;
	}
}

/* End of file */